function asCSV(store) {
    var csv = '';
    // Incude the field names?
    store.fields.each(function(f)
    {
        csv += '"' + f.name + '",';
    });
    csv = csv.replace(/,$/, "") + '\n';
    // Remove the trailing comma, add new line
    // For each record in the store...
    store.each(function(r)
    {
        var recordcsv = '';
        // For each field in the "current" record...
        r.fields.each(function(f)
        {
            recordcsv += '"' + r.get(f.name) + '",';
        });
        csv += recordcsv.replace(/,$/, "") + '\n';
    });
    return csv;
}
function export_data() {

    var csv = asCSV(sampleGrid.getStore());

    form = new Ext.FormPanel({
        //standardSubmit: true,
        fileUpload: true,
        hidden: true,
        url: 'ajax/export.php',
        items: [{
            xtype: 'hidden',
            name: 'data',
            value: csv
        }]
    });
    form.render('search_panel');
    form.getForm().submit()
}

Object.size = function(obj) {
    var size = 0,
    key;
    for (key in obj) {
        if (obj.hasOwnProperty(key)) size++;
    }
    return size;
};

var parse_url = function (uri){
    if (typeof uri == 'undefined') {
        uri = location.href;
    }
    else if (uri[0] == '/'){
        uri = location.host + uri;
    }
    var url = uri.match(/^([^:]*:\/\/)?([^:]*:[^@]*@)?([^\/:]*\.[^\/:]*)?(:[^\/]*)?(\/[^?#]*)?(\?[^#]*)?(#.*)?$/i);
    delete url.input;
    url.protocol = ((url[1])?url[1]:'http://').split('://')[0];
    url.user = (url[2])?url[2].split(':')[0]:undefined;
    url.password = (url[2])?url[2].split(':')[1].split('@')[0]:undefined;
    url.host = (url[3])?url[3]:location.host;
    url.hostname = url.host;
    url.port = (url[4])?((isNaN(parseInt(url[4].split(':')[1])))?80:parseInt(url[4].split(':')[1])):80;
    url.path = (url[5])?url[5]:'/';
    url.pathname = url.path;
    url.search = (url[6])?url[6].split('?')[1]:undefined;
    url.query = url.search;
    url.fragment = (url[7])?url[7].split('#')[1]:undefined;
    url.hash = url.fragment;
    url.href = ''
        + url.protocol + '://'
        + ((url.user)?url.user+':'+url.password+'@':'')
        + url.host
        + ((url.port != 80)?':'+url.port:'')
        + url.path
        + ((url.search)?'?'+url.search:'')
        + ((url.fragment)?'#'+url.fragment:'');
    return url;
}

function cloneObj(o) {
    if (typeof(o) != 'object') return o;
    if (o == null) return o;

    var newO = new Object();

    for (var i in o) newO[i] = cloneObj(o[i]);
    return newO;
}

